package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderList;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderListService;
import com.itheima.utils.SMSUtils;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.List;

@RequestMapping("/orderList")
@Controller
public class OrderListController {

    @Reference
    private OrderListService orderListService;

    /**
     * 查询所有的预约列表
     * @return
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public PageResult findAllOrderList(@RequestBody QueryPageBean queryPageBean){

        try {
            PageResult list= orderListService.findAll(queryPageBean);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 查询有的检查套餐
     * @return
     */
    @RequestMapping("/findAllSetmeal")
    @ResponseBody
    public Result findAllSetmeal(){

        try {
            List<Setmeal> list= orderListService.findAllSetmeal();
            return new Result(true, "成功",list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "获取检查套餐失败!");
    }

    /**
     * 保存预约列表
     * @param setmeal_id
     * @param orderList
     * @return
     */
    @RequestMapping("/saveOrderList")
    @ResponseBody
    public Result saveOrderList(Integer[] setmeal_id,@RequestBody OrderList orderList){

        try {
            orderListService.saveOrderList(setmeal_id,orderList);
            return new Result(true, "保存预约列表成功!!");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new Result(false, "保存预约列表失败!!");
    }


    @RequestMapping("/sendMessage")
    @ResponseBody
    public Result sendMessage(@RequestBody OrderList orderList){

        Date time = orderList.getOrderTime();
        //直接调用发送验证码,个人用户没法申请短信通知的签名,直接用验证码的
        String yyyyMMdd = new SimpleDateFormat("yyyyMMdd").format(time);
        try {
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, "19941147425", yyyyMMdd);
            return new Result(true, "已短信通知用户预约成功!");
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return new Result(false,"通知用户的短信发送失败");
    }

    /**
     * 删除预约列表
     * @param orderList
     * @return
     */
    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(@RequestBody OrderList orderList){
        try {
            int id = orderList.getId(); //主键id
            String phoneNumber = orderList.getPhoneNumber();//手机号
            Date orderTime = orderList.getOrderTime();
            String yyyyMMdd = new SimpleDateFormat("yyyyMMdd").format(orderTime);
            orderListService.deleteOrderListById(id);

            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, phoneNumber, yyyyMMdd);
            return new Result(true, "已通知用户,取消预约!!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "取消预约失败!!");
    }

    /**
     * 批量删除orderlist
     * @param ids 前端传来的主键 数组
     * @return
     */
    @RequestMapping("/deleteByIds")
    @ResponseBody
    public Result deleteByIds(@RequestBody int[] ids){

        if (ids==null || ids.length <=0){
            return new Result(false,"批量删除预约列表失败!!");
        }

        try {
            orderListService.deleteOrderLists(ids);
            return new Result(true, "批量删除预约列表成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,"批量删除预约列表失败!!");
    }

}
